package com.rongji.cms.tools.common.utils.ip;

import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.rongji.cms.tools.common.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;

/**
 * 在线根据ip获取地理位置
 *
 * @author yang
 * @create 2022/11/28
 **/
@Slf4j
public class OnlineIpRegionUtil {

    /**
     * website for query geography address from ip
     */
    public static final String ADDRESS_QUERY_SITE = "http://whois.pconline.com.cn/ipJson.jsp";


    public static IpRegion getIpRegion(String ip) {
        if (StrUtil.isBlank(ip) || IpUtil.isValidIpv6(ip) || !IpUtil.isValidIpv4(ip)) {
            return null;
        }

        // no need to query address for inner ip
        if (NetUtil.isInnerIP(ip)) {
            return new IpRegion("internal", "IP");
        }
        try {
            String rspStr = HttpUtil.get(ADDRESS_QUERY_SITE + "?ip=" + ip + "&json=true",
                    CharsetUtil.CHARSET_GBK);

            if (StrUtil.isEmpty(rspStr)) {
                log.error("获取地理位置异常 {}", ip);
                return null;
            }

            String province = JsonUtils.getAsString(rspStr, "pro");
            String city = JsonUtils.getAsString(rspStr, "city");
            return new IpRegion(province, city);
        } catch (Exception e) {
            log.error("获取地理位置异常 {}", ip);
        }

        return null;
    }

}
